<?php

namespace App\Console\Commands;

use App\Models\CommodityActivity;

class BuildCommodityActivitiesName extends ForEachAllAgents
{
    /**
     * 命令名称及签名
     *
     * @var string
     */
    protected $signature = 'migrate:build_commodity_activities_name';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = '找没有活动名称的活动再根据商品名称作为活动名称';

    public function handle()
    {
        CommodityActivity::withTrashed()
            ->where('name', '')
            ->with('commodity:id,name')
            ->select(['id', 'commodity_id'])
            ->eachById(function ($activity) {
                if (!empty($activity['commodity']['name'])) {
                    $activity->update(['name' => $activity['commodity']['name']]);
                }
            });
    }
}
